<?php
error_reporting (E_ALL);
ini_set('display_errors',TRUE);

	class fluxDelete extends fluxMySql {

		private $table       = NULL;
		private $values      = NULL;
		private $mainObj     = NULL;

		public function __construct ($method,$fields,$mainObj){

			$this->mainObj=$mainObj;

		}

		private function fieldExists ($field=NULL) {

			if ($this->cache) {

				$this->setFieldsInCache();

				foreach ($this->fieldsCache as $k=>$v) { 
					if ($v['Field'] == $field)
						return $v;
				}

				return FALSE;

			}

			foreach ($this->mainObj->getFieldsFrom($this->table) as $k=>$v)
					if ($v['Field'] == $field)
						return $v;

			return FALSE;

		}

		public function getFieldProperties($field){
				  return $this->fieldExists($field);
		}

		public function __call ($field,$value){

			if ($this->fieldExists($field)&&!method_exists($this,'___'.$field.'___')){
					  $this->addField($field,$value);
					  return;
			}

			$this->fieldDoesntExists($field);

		}

		public function getAutoFilterState () {
				  return $this->autoFilter;
		}

		public function setAutoFilter ($state) {
				 $this->autoFilter=$state;
		}

		private function fieldCheck($field,$value){

			$fProperties=$this->getFieldProperties($field);

			if ($fProperties['Key'] == 'PRI'){
					  $msg =  'You cannot use ->' . $field . '<- for inserting ' .
								 'the given field is a PRIMARY KEY' . "\n";
						
					  die ($msg);

			}

			if ($fProperties['Type'] == 'INT' && is_int($value))
					  return FALSE;

			return TRUE;


		}

		public function __toString () {

				  foreach ($this->values as $k => $v){

						$left  = $v['field'].',';

						$quoteValue=$this->fieldCheck ($v['field'],$v['value']);

						if ($this->getAutoFilterState()) {

							if ($quoteValue)	
									  $right ="'". mysql_real_escape_string($v['value'],$this->mainObj->getLink()) . "'". ',';
							else
									  $right = mysql_real_escape_string($v['value'],$this->mainObj->getLink()) .',';
							continue;

						}
						 

						$this->mainObj->writeWarning('Auto filter of fields to be inserted is OFF!');

							if ($quoteValue)	
									  $right ="'".$v['value']."'".',';
							else
									  $right =$v['value'] .',';

				  }

				  $right = substr($right,0,-1);
				  $left  = substr($left,0,-1);

				  $query = 'INSERT INTO ' . $this->table . ' ('.$left.')' . 
							  ' VALUES ' . 
							  '(' . $right . ')' . "\n";

				  return ($query);

		}

	}

?>
